package com.vtest.quartz.utils.others;


import lombok.Data;
import org.apache.commons.mail.SimpleEmail;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import java.util.List;

/**
 * @ClassName MailHelper
 * @Author tiger
 * @Description: 邮件工具类
 * @Date 2024/1/24 15:50
 */
@Data
public class MailUtils {

    //private static String mailUserName = "ruhu.wang@v-test.com.cn";
    private static String mailUserName = "vtest.systembk@v-test.com.cn";

    //private static String mailPassword = "Vt121@098Ruw";
    private static String mailPassword = "VTest88!";

    public MailUtils() {
    }

    public MailUtils(String mailUserName, String mailPassword) {
        this.mailUserName = mailUserName;
        this.mailPassword = mailPassword;
    }

    /**
     * @return void
     * @Description: 只发送消息
     * @Date 2024/1/24 15:56
     * @Param [mailTo, mailCc, subject, message]
     * mailTo : 收件人用 ; 分割
     * mailCc : 抄送人用 ; 分割
     */
    public static void sendMail(String mailTo, String mailCc, String subject, String message) {
        try {
            SimpleEmail simpleEmail = new SimpleEmail();
            simpleEmail.setHostName("smtphz.qiye.163.com");
            simpleEmail.setSmtpPort(25);
            simpleEmail.setTLS(true);
            //simpleEmail.setAuthentication("vtest.systembk@v-test.com.cn", "VTest88!");
            simpleEmail.setAuthentication(mailUserName, mailPassword);  //设置账号密码

            simpleEmail.setFrom(mailUserName); //设置发件人

            String[] mailTos = mailTo.split(";");
            for (String to : mailTos) {
                simpleEmail.addTo(to); //设置收件人
            }

            String[] mailCcs = mailCc.split(";");
            for (String cc : mailCcs) {
                simpleEmail.addCc(cc); //设置抄送人
            }

            simpleEmail.setSubject(subject); //设置邮件标题

            simpleEmail.setMsg(message); //设置内容

            simpleEmail.send();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @return void
     * @Description: 发送消息和一个附件
     * @Date 2024/1/24 16:14
     * @Param [mailTo, mailCc, subject, message, mailFile]
     */
    public static void sendMail(String mailTo, String mailCc, String subject, String message, String mailFile) {
        try {
            SimpleEmail simpleEmail = new SimpleEmail();
            simpleEmail.setHostName("smtphz.qiye.163.com");
            simpleEmail.setSmtpPort(25);
            simpleEmail.setTLS(true);
            //simpleEmail.setAuthentication("vtest.systembk@v-test.com.cn", "VTest88!");
            simpleEmail.setAuthentication(mailUserName, mailPassword);  //设置账号密码

            simpleEmail.setFrom(mailUserName); //设置发件人

            String[] mailTos = mailTo.split(";");
            for (String to : mailTos) {
                simpleEmail.addTo(to); //设置收件人
            }

            String[] mailCcs = mailCc.split(";");
            for (String cc : mailCcs) {
                simpleEmail.addCc(cc); //设置抄送人
            }

            simpleEmail.setSubject(subject); //设置标题


            MimeMultipart mimeMultipart = new MimeMultipart();
            BodyPart textpart = new MimeBodyPart();
            textpart.setContent(message, "text/html;charset=utf-8"); //设置邮件内容模块

            BodyPart excelPart = new MimeBodyPart();
            DataHandler dataHandler1 = new DataHandler(new FileDataSource(mailFile));
            excelPart.setDataHandler(dataHandler1); //设置附件模块
            excelPart.setFileName(MimeUtility.encodeText(dataHandler1.getName()));

            mimeMultipart.addBodyPart(excelPart);
            mimeMultipart.addBodyPart(textpart);

            mimeMultipart.setSubType("mixed");

            simpleEmail.setContent(mimeMultipart);

            simpleEmail.send();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @return void
     * @Description: 发送消息和多个附件
     * @Date 2024/1/24 16:20
     * @Param [mailTo, mailCc, subject, message, mailFiles]
     */
    public static void sendMail(String mailTo, String mailCc, String subject, String message, List<String> mailFiles) {
        try {
            SimpleEmail simpleEmail = new SimpleEmail();
            simpleEmail.setHostName("smtphz.qiye.163.com");
            simpleEmail.setSmtpPort(25);
            simpleEmail.setTLS(true);
            //simpleEmail.setAuthentication("vtest.systembk@v-test.com.cn", "VTest88!");
            simpleEmail.setAuthentication(mailUserName, mailPassword);  //设置账号密码

            simpleEmail.setFrom(mailUserName); //设置发件人

            String[] mailTos = mailTo.split(";");
            for (String to : mailTos) {
                simpleEmail.addTo(to); //设置收件人
            }

            String[] mailCcs = mailCc.split(";");
            for (String cc : mailCcs) {
                simpleEmail.addCc(cc); //设置抄送人
            }

            simpleEmail.setSubject(subject);

            MimeMultipart mimeMultipart = new MimeMultipart();
            BodyPart textpart = new MimeBodyPart();
            textpart.setContent(message, "text/html;charset=utf-8"); //设置邮件内容

            for (String mailFile : mailFiles) {
                BodyPart excelPart = new MimeBodyPart();
                DataHandler dataHandler1 = new DataHandler(new FileDataSource(mailFile));
                excelPart.setDataHandler(dataHandler1);
                excelPart.setFileName(MimeUtility.encodeText(dataHandler1.getName()));
                mimeMultipart.addBodyPart(excelPart);
            }


            mimeMultipart.addBodyPart(textpart);

            mimeMultipart.setSubType("mixed");

            simpleEmail.setContent(mimeMultipart);

            simpleEmail.send();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    public static void main(String[] args) {

    }
}
